Skip to content

Rendering

Renderers turn nodes into the Markdown of a flavour (material, the default, or github). See Markdown flavours for what changes between them.

Example
import mknodes as mk
from mknodes import rendering

node = mk.MkAdmonition("Careful!", typ="warning")
await node.to_markdown(flavor="github")  # > [!WARNING] ...

with rendering.use(rendering.get_renderer("github")):
    str(node)  # nested nodes, templates and str() render in that flavour

mknodes.rendering.get_renderer

get_renderer(flavor: str | Renderer | None = None) -> Renderer

Return a renderer for given flavour name (None: the active renderer).

mknodes.rendering.use

use(renderer: Renderer) -> Generator[Renderer]

Make given renderer the active one inside the context.

mknodes.rendering.Renderer

Renders nodes as Markdown of one flavour.

Subclass it for a new flavour and register rules with the rule decorator. Rule lookup walks the node class hierarchy: a rule for MkBlock also renders MkDetailsBlock, unless a class in between renders itself (defines get_content / to_md_unprocessed) and has no rule of its own. Rules of parent renderer classes are inherited.

name class-attribute

name: str = 'markdown'

Flavour name.

template_key class-attribute

template_key: str | None = None

[output.<key>] section of node files whose template serves as a rule.

finalize

finalize(output: BuildOutput, root: MkNav) -> None

Post-process a whole build (all pages rendered), in place. Default: nothing.

Parameters:

Name Type Description Default
output BuildOutput

The build output

required
root MkNav

The root nav of the build

required

find_rule classmethod

find_rule(node_type: type) -> Rule | None

Return the rule for given node type (None: the node renders itself).

process

process(node: MkNode, text: str) -> str

Run the text processors of given node on given text.

processors

processors(node: MkNode) -> list[TextProcessor]

Return the text processors to run on the Markdown of given node.

render async

render(node: MkNode) -> NodeContent

Render given node (unprocessed Markdown) and collect its resources.

render_child async

render_child(node: MkNode) -> NodeContent

Render a child node: content with the node's processors applied.

render_children async

render_children(nodes: list[MkNode], separator: str = '\n\n') -> str

Render given nodes as children and join their Markdown.

render_markdown async

render_markdown(node: MkNode) -> str

Render the unprocessed Markdown of given node.

rule classmethod

rule(*node_types: type) -> Callable[[R], R]

Decorator registering a render function for given node types.

supports classmethod

supports(node_type: type) -> bool

Return whether this renderer has an own rule for given node type.

to_html

to_html(markdown: str, reqs: Resources | None = None) -> str

Convert Markdown of this flavour to HTML (with a parser knowing the flavour).

The default uses Python-Markdown with the Markdown extensions of given resources (Material flavour). See mknodes.rendering.html.

mknodes.rendering.MaterialRenderer

Bases: Renderer

Material for MkDocs flavour (the default): nodes render themselves.

mknodes.rendering.github.GithubRenderer

Bases: Renderer

Renders GitHub flavoured Markdown (READMEs, issues, wikis).

finalize

finalize(output: BuildOutput, root: MkNav) -> None

Link [title][dotted.path] references (GitHub has no autorefs).

mknodes.rendering.myst.MystRenderer

Bases: GithubRenderer

Renders MyST Markdown for Sphinx (myst-parser + sphinx-design).

finalize

finalize(output: BuildOutput, root: MkNav) -> None

Link [title][dotted.path] references, add MyST targets for the anchors.

Page info
Code for this page
mknodes.manual.api_section._add_page.<locals>._
@nav.route.page(title, hide="toc")
def _(page: mk.MkPage) -> None:
    page += INTROS[title]
    page += mk.MkCode(EXAMPLES[title].rstrip(), language="py", title="Example")
    for identifier in identifiers:
        page += mk.MkDocStrings(identifier, **DOCSTRING_OPTIONS)
Resources
Resources(css=[],
          markdown_extensions={'pymdownx.highlight': {'anchor_linenums': True,
                                                      'line_spans': '__span',
                                                      'pygments_lang_class': True},
                               'pymdownx.superfences': {}},
          plugins=[Plugin('mkdocstrings')],
          js=[],
          assets=[],
          packages=[])
Metadata
created:
  source_filename: /home/runner/work/mknodes/mknodes/src/mknodes/manual/api_section.py
  source_function: _add_page.<locals>._
  source_line_no: 168
hide:
- toc
title: Rendering